﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireFinalReportBLL
    {
        #region 构造单例
        private FireFinalReportBLL() { }
        private static FireFinalReportBLL _instance;
        public static FireFinalReportBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireFinalReportBLL());
            }
        }
        #endregion

        public ResultDto<List<FireMQTTResponseModel>> Add(Firefinalreport info)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();


            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {


                var fireInfo = BLL.FireInfoBLL.Instance.Get(info.FireId);
                if (fireInfo == null)
                {
                    result.Status = -1;
                    result.Message = "FireInfo Is Not Exist";
                }

                var existFireFinalReport =ef.Firefinalreport.Where(p=>p.FireId==info.FireId).FirstOrDefault();
                if (existFireFinalReport != null)
                {
                    result.Status = -2;
                    result.Message = "FireFinalReport Is Not Exist";
                }

                ef.Firefinalreport.Add(info);
                ef.Entry<Firefinalreport>(info).State = EntityState.Added;
                ef.SaveChanges();

                //关闭火灾
                fireInfo.IsState = 1;
                ef.Fireinfo.Add(fireInfo);
                ef.Entry<Fireinfo>(fireInfo).State = EntityState.Modified;
                ef.SaveChanges();

                string transDatetime = info.TransDatetime.ToString("yyyy-MM-dd HH:mm:ss");
                Record record = new Record();
                record.FireId = fireInfo.FireId;
                record.HandleTime = info.TransDatetime;
                record.Type = (int)RecordType.火灾终报;
                record.UserId = info.UserId;
                string content = string.Format(FireInfoTool.GetFireInfoRecordFormat(record.Type.Value), fireInfo.FireName, transDatetime);
                record.Content = content;
                record.AddTime = info.TransDatetime;
                record.Ip = info.Ip;
                record.Applicant = fireInfo.Alarm;
                record.CustomData = info.FinalReportId.ToString();
               
                ef.Record.Add(record);
                ef.Entry<Record>(record).State = EntityState.Added;

                Message message = new Message();
                message.FireId = fireInfo.FireId;
                message.Type = (int)MessageType.火灾终报;
                message.Content = content;
                message.SendType = (int)MessageSendType.下级向上级发送;

                //特殊附带参数
                message.CustomData = info.FinalReportId.ToString();
                message.IsRead = 0;
                message.HandleTime = info.TransDatetime;
                message.Ip = fireInfo.Ip;
                ef.Message.Add(message);
                ef.Entry<Message>(message).State = EntityState.Added;
                ef.SaveChanges();

                #region 装载消息
                List<Message> messages = new List<Message>() { message };
                result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages, record.Type.Value);
                #endregion
                return result;
            }
           
        }

        public Firefinalreport Get(int fireId)
        {
            return null;
        }
    }
}
